package com.controller;

import com.github.pagehelper.PageInfo;
import com.pojo.Department;
import com.pojo.User;
import com.service.DepartmentService;
import com.service.UserService;
import com.vo.Result;
import com.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/teammanager")
public class TeamManager {
    @Resource
    private DepartmentService departmentService;
    @Resource
    private UserService userService;

    @RequestMapping("/depart")
    public  String todepart(){
        return "TeamManager/department";
    }

    @RequestMapping("/depart/show")
    @ResponseBody
    public Result departshow(){
        List<Department> selectdepartall = departmentService.selectdepartall();
        return Result.success(selectdepartall);
    }
    @RequestMapping("/depart/add")
    @ResponseBody
    public Result departadd(Department department){
        System.out.println(department);
        try {
            int i = departmentService.insertSelective(department);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.success("添加失败");
        }

    }
    @RequestMapping("/user")
    public  String touser(){
        return "TeamManager/user";
    }

    @RequestMapping("/user/show")
    @ResponseBody
    public Result usershow(UserVo userVo){
        PageInfo<User> userPageInfo = userService.selectUserall(userVo);
        return Result.success(userPageInfo);
    }
    @RequestMapping("/user/add")
    @ResponseBody
    public Result useradd(User user){
        int i = userService.insertSelective(user);
        if (i>0){
            return Result.success("添加成功");
        }else {
            return Result.success("添加失败");
        }

    }
    @RequestMapping("/user/update")
    @ResponseBody
    public Result userupdate(User user){
        System.out.println(user);
        int i = userService.updateByPrimaryKeySelective(user);
        if (i>0){
            return Result.success("修改成功");
        }else {
            return Result.success("修改失败");
        }

    }
}
